package com.fivestar.pmanager.core.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * spring 工具类
 *
 */
@SuppressWarnings("unchecked")
public class SpringUtil implements ApplicationContextAware
{
    private static ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException
    {
        SpringUtil.applicationContext = arg0;
    }
    
    /**
     * 根据类型获取bean
     *
     * @param bean 类型
     * @return
     * @throws
     */
    public static <T> T getBean(Class<T> clazz)
    {
        return applicationContext.getBean(clazz);
    }
    
    
    /**
     * 根据beanname获取bean
     *
     * @param name beanName
     * @return
     * @throws
     */
    public static Object getBean(String name)
    {
        return applicationContext.getBean(name);
    }
    
    
    /**
     * 根据beanname获取bean和类型获取
     *
     * @param name beanName
     * @return
     * @throws
     */
    public static <T> T getBean(String name ,  Class<T> clazz)
    {
        return applicationContext.getBean(name,clazz);
    }
    
   
}
